//
//  JGroupOrderCell.m
//  HeadIndustry
//
//  Created by jun on 2017/9/11.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JGroupOrderCell.h"
#import "JPayProductView.h"


@interface JGroupOrderCell()

@property (nonatomic, assign) NSInteger productNum;
@property (nonatomic, strong) NSMutableArray *cellViewArr;
@property (nonatomic, assign) CGFloat lastMaxY;
@property (nonatomic, strong) UILabel *verdonNameLa;
@property (nonatomic, strong) CALayer *rightImg;
@property (nonatomic, assign) BOOL isFirst;
@property (nonatomic, strong) YYLabel *statuela;
//显示照片
@property (nonatomic,strong) UIImageView *ImageView;
//商品名
@property (nonatomic,strong) YYLabel *nameLabel;
//尺寸
@property (nonatomic,strong) YYLabel *sizeLabel;
//时间
@property (nonatomic,strong) YYLabel *dateLabel;
//价格
@property (nonatomic,strong) YYLabel *priceLabel;
//数量
@property (nonatomic,strong)YYLabel *numberLabel;

//时间
@property (nonatomic,strong) UILabel *couponLabel;
//价格
@property (nonatomic,strong) UILabel *SumPriceLabel;
//数量
//@property (nonatomic,strong)UILabel *numberLabel;

@end
@implementation JGroupOrderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.opaque = NO;
        _isFirst = NO;
        self.backgroundColor = [UIColor whiteColor];
        [self configBgCellView];
        [self setUpView];
    }
    return self;
}

- (void)configBgCellView
{
    UIView *topView = [[UIView alloc]init];
    topView.frame = CGRectMake(0, 0, self.contentView.width, 45);
    [self.contentView addSubview:topView];
    
    CALayer *layer = [CALayer layer];
    layer.contents = (__bridge id _Nullable)([UIImage imageNamed:@"hgome"].CGImage);
    layer.frame = CGRectMake(15, 11, 20, 20);
    [topView.layer addSublayer:layer];
    
    UILabel *namela = [UILabel new];
    namela.frame = CGRectMake(layer.right + 3, 0, 80, topView.height);
    namela.font = kFONT_TITLE(16);
    [topView addSubview:namela];
    namela.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (_block) {
            _block(10, self.groupModel);
        }
    }];
    [namela addGestureRecognizer:tap];
    _verdonNameLa = namela;
    
    CALayer *rightImg = [CALayer layer];
    rightImg.contents = (__bridge id _Nullable)([UIImage imageNamed:rightBtnImg].CGImage);
    rightImg.frame = CGRectMake(namela.right + 3, (45 - 16)/2.0, 10, 16);
    [topView.layer addSublayer:rightImg];
    _rightImg = rightImg;
    
    YYLabel *statuLa = [YYLabel new];
    statuLa.frame = CGRectMake(kWindowW - 15 - 100, 0, 100 , 40);
    statuLa.font = kFONT_TITLE(16);
    statuLa.textColor = [UIColor redColor];
    statuLa.textAlignment = NSTextAlignmentRight;
    [topView addSubview:statuLa];
    statuLa.text = [NSString stringWithFormat:@"%@",_groupModel.ServerOrderStatusText];
    _statuela = statuLa;
    
}

- (void)setUpView {
    UIView *midView = [[UIView alloc]initWithFrame:CGRectMake(0, 45, kWindowW, 100)];
    midView.backgroundColor = kRGBColor(245, 245, 245);
    [self.contentView addSubview:midView];
    
    //显示照片
    UIImageView* imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"default_pic_1"];
    imageView.frame = CGRectMake(15,  5, 85, 85);
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [midView addSubview:imageView];
    self.ImageView = imageView;
    
    CGFloat width = midView.width - imageView.right - 30 ;
    
    //商品名
    YYLabel* nameLabel = [[YYLabel alloc]init];
    nameLabel.frame = CGRectMake(imageView.right + 10, 10, width, 40);
    nameLabel.font = [UIFont systemFontOfSize:15];
    nameLabel.numberOfLines = 0;
    [midView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    //尺寸
    YYLabel* sizeLabel = [[YYLabel alloc]init];
    sizeLabel.frame = CGRectMake(nameLabel.left, nameLabel.bottom, width, 20);
    sizeLabel.textColor = kRGBColor(150, 150, 150);
    sizeLabel.font = [UIFont systemFontOfSize:12];
    [midView addSubview:sizeLabel];
    self.sizeLabel = sizeLabel;
    
    //单价
    YYLabel* dateLabel = [[YYLabel alloc]init];
    dateLabel.frame = CGRectMake(nameLabel.left, sizeLabel.bottom , width, 20);
    dateLabel.font = [UIFont systemFontOfSize:12];
    dateLabel.textColor = [UIColor redColor];
    [midView addSubview:dateLabel];
    self.dateLabel = dateLabel;
    
    //数量
    YYLabel* priceLabel = [[YYLabel alloc]init];
    priceLabel.frame = CGRectMake(midView.right - width - 10, sizeLabel.bottom, width, 20);
    priceLabel.font = [UIFont boldSystemFontOfSize:12];
    priceLabel.textColor = [UIColor blackColor];
    priceLabel.textAlignment = NSTextAlignmentRight;
    [midView addSubview:priceLabel];
    self.priceLabel = priceLabel;
    
    [self configButtomView:45 + 100];
}

- (void)configButtomView:(CGFloat)minY
{
//    UILabel *productFreight = [UILabel new];
//    productFreight.frame = CGRectMake(kWindowW - 15 - 100, minY, 100 , 40);
//    productFreight.font = kFONT_TITLE(14);
//    [self.contentView addSubview:productFreight];
//    productFreight.text = [NSString stringWithFormat:@"抵用券 %@ 元",_groupModel.CouponPriceText];
//    _couponLabel = productFreight;
    
    // 小计
    UILabel *SumPriceLa = [UILabel new];
    SumPriceLa.frame = CGRectMake(kWindowW - 15 - 80, minY, 80, 40);
    SumPriceLa.font = kFONT_TITLE(14);
    SumPriceLa.textColor = [UIColor redColor];
    [self.contentView addSubview:SumPriceLa];
    SumPriceLa.text = [NSString stringWithFormat:@"合计:%@元",_groupModel.PriceText];
    _SumPriceLabel = SumPriceLa;
    // 小计
//    YYLabel *SumPriceLaText = [YYLabel new];
//    SumPriceLaText.frame = CGRectMake(SumPriceLa.left - 40, minY, 40, 40);
//    SumPriceLaText.font = kFONT_TITLE(14);
//    [self.contentView addSubview:SumPriceLaText];
//    SumPriceLaText.text = @"合计:";
    
    UILabel *productNumLa = [UILabel new];
    productNumLa.frame = CGRectMake(15, minY, SumPriceLa.left - 60 - 15 , 40);
    productNumLa.font = kFONT_TITLE(14);
    productNumLa.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:productNumLa];
    productNumLa.text = [NSString stringWithFormat:@"抵用券 %@ 元",_groupModel.CouponPriceText];
    _couponLabel = productNumLa;
    
    CALayer *line3 = [CALayer layer];
    line3.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line3.frame = CGRectMake(0, productNumLa.bottom - 0.5, kWindowW, 0.5);
    [self.contentView.layer addSublayer:line3];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(kWindowW - 15 - 75, minY + 45, 75, 40);
    [btn1 setTitle:@"付款" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn1.titleLabel.font = kFONT_TITLE(16);
    
    btn1.layer.cornerRadius = 5;
    btn1.layer.borderColor = [UIColor redColor].CGColor;
    btn1.layer.borderWidth = 1;
    btn1.tag = 1;
    [btn1 addTarget:self action:@selector(payBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake( btn1.left - 10 - 75, minY + 45, 75, 40);
    [btn2 setTitle:@"取消订单" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn2.titleLabel.font = kFONT_TITLE(16);
    
    btn2.layer.cornerRadius = 5;
    btn2.layer.borderColor = [UIColor blackColor].CGColor;
    btn2.layer.borderWidth = 1;
    btn2.tag = 2;
    [btn2 addTarget:self action:@selector(payBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn2];
    
    CALayer *line4 = [CALayer layer];
    line4.backgroundColor = kRGBColor(150, 150, 150).CGColor;
    line4.frame = CGRectMake(0, productNumLa.bottom + 50 - 0.5, kWindowW, 0.5);
    [self.contentView.layer addSublayer:line4];
}


- (void)setGroupModel:(JOrderGroupModel *)groupModel
{
    _groupModel = groupModel;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        _statuela.text = [NSString stringWithFormat:@"%@",_groupModel.OrderStatusName];
        _verdonNameLa.text = groupModel.ShopName;
      
        _SumPriceLabel.text = [NSString stringWithFormat:@"合计:%@元",_groupModel.PayablePriceText];
        _couponLabel.text = [NSString stringWithFormat:@"抵用券:-%@元",_groupModel.CouponPriceText];
        
        self.ImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:groupModel.PictureUrl]]];

        self.nameLabel.text = groupModel.ServerName;
        self.priceLabel.text = [NSString stringWithFormat:@"X%ld",(long)groupModel.Qty];
        self.dateLabel.text = groupModel.PriceText;
        
        NSMutableString *mStr = [NSMutableString string];
        [mStr appendString:[NSString stringWithFormat:@"特性:"]];
        [mStr appendString:[NSString stringWithFormat:@"%@",groupModel.Type]];
        [mStr appendString:[NSString stringWithFormat:@" "]];
        [mStr appendString:[NSString stringWithFormat:@"%@",groupModel.TypeName]];

        self.sizeLabel.text = mStr;
        
        
        [_SumPriceLabel sizeToFit];
        [_couponLabel sizeToFit];
        [_verdonNameLa sizeToFit];
        CGFloat height = _verdonNameLa.height;
        CGFloat widht = _verdonNameLa.width;
        _SumPriceLabel.frame = CGRectMake(kWindowW - 15 - _SumPriceLabel.width, _SumPriceLabel.frame.origin.y, _SumPriceLabel.width, 40);
        
        _couponLabel.frame = CGRectMake(15, _SumPriceLabel.frame.origin.y, _SumPriceLabel.left - 15 - 10 , 40);
        _verdonNameLa.frame = CGRectMake(38, (45 - height)/2.0, widht, _verdonNameLa.height);
        _rightImg.frame = CGRectMake(_verdonNameLa.right + 3, _rightImg.origin.y, _rightImg.width, _rightImg.height);
        
        
        
    });
}

- (void)payBtnClick:(UIButton *)btn
{
    if (_block) {
        _block(btn.tag,self.groupModel);
    }
}

- (CGFloat)getHeight
{
    return _lastMaxY + 40;
}

- (NSMutableArray *)cellViewArr
{
    if (_cellViewArr == nil) {
        _cellViewArr = [NSMutableArray array];
    }
    return _cellViewArr;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
